#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/7/26 16:40
# @Author : 闫峰
# @File    : SyncMarketProductCategoryInvalid.py
import os

from common.baserequest import Base_request
from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])
info_path = ensure_path_sep(f"\\logs\\info-{dis_path}.log")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}.log")
if os.path.exists(info_path):
    os.remove(info_path)
if os.path.exists(error_path):
    os.remove(error_path)
if os.path.exists(warning_path):
    os.remove(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')

class TestSyncMarketProductCategoryInvalid:
    znkd_db = Database(ensure_path_sep("\\config\\znkd_mysql_config.yaml"), "znkd_database_prod")

    def test_SyncMarketProductCategoryInvalid(self,MarketId):
        platform_id_list = [97,58,70,45,67,57]

        # 首先是验证 m_productcategory表是否有更新时间是一个月之前的类目
        # 遍历m_productcategory_invalid表 验证在m_productcategory不存在 以及在属性表 属性值表中的删除
        sql1 = f"""SELECT MarketId,SiteCode,CategoryId FROM m_productcategory 
        WHERE MarketId={MarketId} AND LastModificationTime <= DATE_SUB(NOW(), INTERVAL 30 DAY);"""
        res1 = self.znkd_db.fetch_all(sql1, ())
        if res1:
            for data in res1:

                ERROR.logger.error(f"MarketId={MarketId},SiteCode={data.get('SiteCode')},CategoryId={data.get('CategoryId')} 未删除")
        else:
            pass

    def test002(self,MarketId):
        marketId_mapping = {
            57:"m_aliexpress_attribute",
            58:"m_lazada_attribute",
            67:"m_allegro_attribute",
            45:"m_walmart_attribute",
            70:"m_mercadolibre_attribute",
            97:"m_shopee_attribute"
        }
        sql2 = f"""
        SELECT CategoryId FROM m_productcategory_invalid WHERE MarketId={MarketId} AND SiteCode=%s AND CategoryId=%s;
        """
        res2 = self.znkd_db.fetch_all(sql2, ())
        if res2:
            for data in res2:
                sql3 = f"""
                SELECT * FROM {marketId_mapping.get(MarketId)} WHERE CustomerId=1 AND CategoryId=%s;
                """
                sql4 = f"""SELECT * FROM {marketId_mapping.get(MarketId)}_value WHERE CustomerId=1 AND CategoryId=%s"""
                res3 = self.znkd_db.fetch_all(sql3, (data.get('CategoryId')),)
                if res3:
                    ERROR.logger.error(f"{marketId_mapping.get(MarketId)} 表未清除干净数据，CategoryId= {data.get('CategoryId')}")
                res4 = self.znkd_db.fetch_all(sql4, (data.get('CategoryId')),)
                if res4:
                    ERROR.logger.error(
                        f"{marketId_mapping.get(MarketId)}_value 表未清除干净数据，CategoryId= {data.get('CategoryId')}")

if __name__ == '__main__':
    pass